﻿//创建时间: 2010-10-13
//创建作者: 李晓忠
//功能说明: CMS系统 文件浏览选择 
//修改: 2010-10-14
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;

using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using Newtonsoft.Json;

namespace TRPOP.CMS.Web.Admin
{
    public partial class AdminFileSelectWork : BaseAdmin
    {

        #region 页面变量
        protected string m_strAction = "";
        protected string m_strPath = "";
        protected string m_strSelPath = "";
        protected string m_strExtName = "";
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            m_strAction = RequestHelper.RequestString("ac");
            m_strPath = RequestHelper.RequestString("path").Replace("//", "/");
            m_strSelPath = RequestHelper.RequestString("selpath");
            m_strExtName = RequestHelper.RequestString("extname");
            if (m_strExtName.StartsWith("|") == false)
                m_strExtName = "|" + m_strExtName;
            if (m_strExtName.EndsWith("|") == false)
            {
                m_strExtName = m_strExtName + "|";
            }

            if (m_strAction.IndexOf("ajax") > -1)
            {
                AjaxMethod();
            }
            else
            {

                if (!IsPostBack)
                {
                    LoadBase();
                    BindData();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {




        }
        #endregion

        #region 页面方法

        public void LoadBase()
        {


        }

        public void BindData()
        {
            JavaScriptObject result = new JavaScriptObject();
            string strTempletePaht = SystemInfoHelper.getSystemInfo().CMSRoot.Replace("/", "") + "/" + m_strPath;
            result = FileUtil.getJsonFileList(Common.Util.GetMapPath(strTempletePaht), m_strExtName);

            if (result.ContainsKey("err"))
            {
                filelist.InnerHtml = result["err"].ToString();
            }
            else
            {
                //目录
                if (result.ContainsKey("dirlist"))
                {
                    JavaScriptObject dirlist = (JavaScriptObject)result["dirlist"];
                    foreach (string key in dirlist.Keys)
                    {
                        filelist.InnerHtml += getForder(key);
                    }
                }
                //文件
                if (result.ContainsKey("filelist"))
                {
                    JavaScriptObject flist = (JavaScriptObject)result["filelist"];
                    foreach (string key in flist.Keys)
                    {
                        JavaScriptObject jsFile = (JavaScriptObject)flist[key];
                        if (jsFile.ContainsKey("extname"))
                            filelist.InnerHtml += getFile(jsFile["name"].ToString(), jsFile["extname"].ToString());
                    }
                }
            }

        }

        protected string getForder(string name)
        {
            string str = "";
            // str += "<div class=\"work_forder\" title=\""+name+"\"  ondblclick=\"opendir('"+name+"')\" onclick=\"divClick(this,'"+name+"')\" ><img src=\""+SystemInfoHelper.getSystemInfo().CMSRoot+"App_Themes/cmsware/images/folder.gif\" alt=\""+name+"\"/>" + name + "</div>";

            str += "<div class=\"work_forder\" title=\"" + name + "\"   ondblclick=\"opendir('" + name + "')\" onclick=\"divClick(this,'" + name + "')\" >\r\n";
            str += "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"2\">\r\n";
            str += "<TR>\r\n";
            str += "<TD width=16 height=16><img src=\"" + SystemInfoHelper.getSystemInfo().CMSRoot + "App_Themes/" + TRPOP.CMS.Model.SystemInfoHelper.getSystemInfo().CMSThemes + "/images/folder.gif\" alt=\"" + name + "\" width=\"16\" height=\"16\" ></TD>\r\n";
            str += "<TD>\r\n";
            str += "<div   class=\"imageholder\" >" + name + "</div>\r\n";
            str += "</TD>\r\n";
            str += "</TR>\r\n";
            str += "</TABLE>\r\n";
            str += "</div>\r\n";

            return str;
        }

        private string getFile(string name, string extname)
        {
            string str = "";
            //str += "<div class=\"work_forder\" title=\"" + name + "\"   onclick=\"divClick(this,'" + name + "')\" ><img src=\"" + SystemInfoHelper.getSystemInfo().CMSRoot + "App_Themes/Classic/images/" + getExtName(extname) + ".png\" alt=\"" + name + "\"/>" + name + "</div>";
            str += "<div class=\"work_forder\" title=\"" + name + "\"   onclick=\"divClick(this,'" + name + "')\" ondblclick=\"filedblclick();\" >\r\n";
            str += "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"2\">\r\n";
            str += "<TR>\r\n";
            str += "<TD width=16 height=16><img src=\"" + SystemInfoHelper.getSystemInfo().CMSRoot + "App_Themes/" + TRPOP.CMS.Model.SystemInfoHelper.getSystemInfo().CMSThemes + "/images/" + getExtName(extname) + ".png\" alt=\"" + name + "\" width=\"16\" height=\"16\" ></TD>\r\n";
            str += "<TD>\r\n";
            str += "<div class=\"imageholder\" >" + name + "</div>\r\n";
            str += "</TD>\r\n";
            str += "</TR>\r\n";
            str += "</TABLE>	\r\n";
            str += "</div>\r\n";

            return str;
        }

        private string getExtName(string extname)
        {
            string str = "";
            switch (extname.ToLower())
            {
                case "html":
                case "htm":
                    str = "html";
                    break;
                case "flash":
                    str = "flash";
                    break;
                case "exe":
                    str = "application";
                    break;
                case "css":
                    str = "css";
                    break;
                case "doc":
                case "docx":
                    str = "doc";
                    break;
                case "ppt":
                case "pptx":
                    str = "ppt";
                    break;
                case "cs":
                case "aspx":
                    str = "code";
                    break;
                case "js":
                    str = "script";
                    break;
                case "txt":
                    str = "txt";
                    break;
                case "pdf":
                    str = "pdf";
                    break;
                case "":
                    break;
                default:
                    str = "file";
                    break;
            }
            return str;
        }

        private void AjaxMethod()
        {
            JavaScriptObject result = new JavaScriptObject();
            string m_strPath = "";
            if (m_strAction == "ajax_path")
            {
                m_strPath = RequestHelper.RequestString("path");

                string strTempletePaht = SystemInfoHelper.getSystemInfo().CMSRoot.Replace("/", "") + "/" + m_strPath;
                result = FileUtil.getJsonFileList(Common.Util.GetMapPath(strTempletePaht), "|.html|.htm|.aspx|.asp|");

                if (result.ContainsKey("err"))
                {
                    result.Add("result", "-1");
                    result.Add("msg", "error");
                }
                else
                {
                    result.Add("result", "1");
                    result.Add("msg", "ok");
                }
            }
            else
            {
                result.Add("result", "1");
                result.Add("msg", "ok");
            }

            Response.Write(JavaScriptConvert.SerializeObject(result));

            base.End();
        }
        #endregion
    }
}
